<?php
class Admin_Model_Contact extends Core_Database
{
    /**
     * Lay danh sach tat ca cac lien he
     * @param: void
     * @return : array(): neu co it nhat mot lien he  null: neu khong co lien he nao
     * 
     * */
    public function getContacts()
    {
     $sql = 'select * from contacts order by contact_id DESC';
     $sth = $this->_db->prepare($sql);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;   
    }
    
     /**
     * Lay mot lien he
     * @param : contact_id
     * @return : array : neu ton tai  null : neu khong ton tai
     * */
    public function getContact($contact_id)
    {
        $sql = 'select * from contacts where contact_id = :contact_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':contact_id',$contact_id,PDO::PARAM_INT);
        $status = $sth->execute();
        if (!$status)
            return null;
        $result = $sth->fetchAll();
        if(count($result)>0)
           return $result;
        else
           return null;  
    }
    
    /**
     * Cap nhat tinh trang lien he da duoc doc
     * @param : contact_id
     * @return bool true : thanh cong , false: that bai
     * */
     public function updateUnread($contact_id)
     {
        $sql = 'update contacts set unread = 0 where contact_id = :contact_id';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':contact_id',$contact_id,PDO::PARAM_INT);
    	return $sth->execute();
     }
     
     /**
      * Xoa mot lien he
      * @param :contact_id
      * @return bool  true: thanh cong false: that bai
      * */
      public function deleteContact($contact_id)
      {
        $sql = 'delete from contacts where contact_id = :contact_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':contact_id',$contact_id,PDO::PARAM_INT);
        return $sth->execute();
      } 
}